﻿using FluentValidation;
using Mes.Domain.BoundedContext;
using Microsoft.AspNetCore.Mvc;
using Mes.Application.Common;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.DTO.Results.MasterData;
using Mes.Application.Services.MasterData;

namespace Mes.Application.Web.Controllers.MasterData
{
    /// <summary>
    /// 物料类控制器
    /// </summary>
    [ApiController]
    [Route("/api/v1")]
    public class MaterialClassController : ControllerBase
    {
        private readonly IMaterialClassService _materialClassService;
        private readonly IValidator<AddMaterialClassInput> _addMaterialClassInputValidator;
        private readonly IValidator<GetMaterialClassesInput> _getMaterialClassesInputValidator;
        private readonly IValidator<UpdateMaterialClassInput> _updateMaterialClassInputValidator;
        /// <summary>
        /// 物料类控制器
        /// </summary>
        /// <param name="materialClassService"></param>
        /// <param name="addMaterialClassInputValidator"></param>
        /// <param name="getMaterialClassesInputValidator"></param>
        /// <param name="updateMaterialClassInputValidator"></param>
        public MaterialClassController(IMaterialClassService materialClassService, IValidator<AddMaterialClassInput> addMaterialClassInputValidator, IValidator<GetMaterialClassesInput> getMaterialClassesInputValidator, IValidator<UpdateMaterialClassInput> updateMaterialClassInputValidator)
        {
            _materialClassService = materialClassService;
            _addMaterialClassInputValidator = addMaterialClassInputValidator;
            _getMaterialClassesInputValidator = getMaterialClassesInputValidator;
            _updateMaterialClassInputValidator = updateMaterialClassInputValidator;
        }

        /// <summary>
        /// 新增物料类
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("materialclass")]
        public async Task<ActionResult> AddMaterialClassAsync([FromBody] AddMaterialClassInput input)
        {
            try
            {
                var result = await _addMaterialClassInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _materialClassService.AddMaterialClassAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑物料类
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPatch("materialclass")]
        public async Task<ActionResult> UpdateMaterialClassAsync([FromBody] UpdateMaterialClassInput input)
        {
            try
            {
                var result = await _updateMaterialClassInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _materialClassService.UpdateMaterialClassAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除物料类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("materialclass")]
        public async Task<ActionResult> DeleteMaterialClassesAsync(IList<int> ids)
        {
            try
            {
                await _materialClassService.DeleteMaterialClassesAsync(ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询物料类
        /// </summary>
        /// <param name="input"></param>
        /// <returns>PagedItemsData<MaterialClassResult></returns>
        [HttpGet("materialclass")]
        public async Task<ActionResult<PagedItemsData<MaterialClassResult>>> GetMaterialClassesAsync([FromQuery] GetMaterialClassesInput input)
        {
            try
            {
                var result = await _getMaterialClassesInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                var response = await _materialClassService.GetMaterialClassesAsync(input);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}
